package cn.iocoder.yudao.module.ym.service.worktemp;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ym.controller.admin.worktemp.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.worktemp.WorkTempDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ym.dal.mysql.worktemp.WorkTempMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.WORK_TEMP_NOT_EXISTS;

/**
 * 工单模板 Service 实现类
 *
 * @author 韩艳阳
 */
@Service
@Validated
public class WorkTempServiceImpl implements WorkTempService {

    @Resource
    private WorkTempMapper workTempMapper;

    @Override
    public Long createWorkTemp(WorkTempSaveReqVO createReqVO) {
        // 插入
        WorkTempDO workTemp = BeanUtils.toBean(createReqVO, WorkTempDO.class);
        workTempMapper.insert(workTemp);
        // 返回
        return workTemp.getId();
    }

    @Override
    public void updateWorkTemp(WorkTempSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkTempExists(updateReqVO.getId());
        // 更新
        WorkTempDO updateObj = BeanUtils.toBean(updateReqVO, WorkTempDO.class);
        workTempMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkTemp(Long id) {
        // 校验存在
        validateWorkTempExists(id);
        // 删除
        workTempMapper.deleteById(id);
    }

    private void validateWorkTempExists(Long id) {
        if (workTempMapper.selectById(id) == null) {
            throw exception(WORK_TEMP_NOT_EXISTS);
        }
    }

    @Override
    public WorkTempDO getWorkTemp(Long id) {
        return workTempMapper.selectById(id);
    }

    @Override
    public List<WorkTempDO> getWorkTempList(WorkTempPageReqVO pageReqVO) {
        return workTempMapper.selectList(pageReqVO);
    }

    @Override
    public PageResult<WorkTempDO> getWorkTempPage(WorkTempPageReqVO pageReqVO) {
        return workTempMapper.selectPage(pageReqVO);
    }

}